package com.sgb.kjwl.view.ui.activity.self.personmanger;

import android.content.Context;
import android.content.Intent;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPersonMangerBinding;
import com.sgb.kjwl.di.self.DaggerSelfComponent;
import com.sgb.kjwl.di.self.SelfModule;
import com.sgb.kjwl.model.entity.PersonInfoEntity;
import com.sgb.kjwl.view.ui.adapter.personmanger.PersonMangerAdapter;
import com.swgk.core.dialog.RoundCornerDialog;
import com.sgb.kjwl.viewmodel.self.PersonMangerModel;
import com.swgk.core.base.di.AppComponent;
import com.swgk.core.recyclerview.adapter.AdapterOnItemClick;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.xrecyclerview.XRecyclerView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;

public class PersonMangerActivity extends AppActivity {
    private ActivityPersonMangerBinding binding;
    private PersonMangerAdapter adapter;
    private List<PersonInfoEntity> stringList;
    private Map map;
    private Map deletePerson;
    @Inject
    PersonMangerModel viewModel;
    private String id;

    @Override
    protected void setupActivityComponent(AppComponent appComponent) {
        DaggerSelfComponent.builder().appComponent(appComponent).selfModule(new SelfModule(this)).build().inject(this);
    }

    public static void start(Context context, String id) {
        Intent intent = new Intent();
        intent.putExtra("id", id);
        intent.setClass(context, PersonMangerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_person_manger);
        binding.include.ivBack.setVisibility(View.VISIBLE);
        binding.include.ivBack.setOnClickListener(this);
        binding.tvAddPerson.setOnClickListener(this);
        binding.include.titleView.setText("员工管理");
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        if (adapter == null) {
            adapter = new PersonMangerAdapter(getActivity(), null);
        }
        binding.rlvList.setLayoutManager(layoutManager);
        binding.rlvList.setAdapter(adapter);
        adapter.setOnItemClick(new AdapterOnItemClick<PersonInfoEntity>() {
            @Override
            public void onItemClick(PersonInfoEntity s, int position) {
//                showDialog(s.getId(), position);
            }
        });
        adapter.Idelete((id, position) -> {
            showDialog(id, position);
        });
        binding.rlvList.setLoadingMoreEnabled(false);
        binding.rlvList.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                viewModel.refresh(map);
            }

            @Override
            public void onLoadMore() {
//                viewModel.lodaMore();
            }
        });


    }


    @Override
    protected void init() {
        id = getIntent().getStringExtra("id");
        SharedPreferenceUtil.init(this);
        map = new HashMap();
        deletePerson = new HashMap();
        map.put("mobile", SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_PHONE, ""));
        viewModel.refresh(map);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_add_person:
                AddPersonActivity.start(this, "");
                break;
        }
    }

    private void initDatas() {
        stringList = new ArrayList<>();
       /* stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");
        stringList.add("测试数据");*/
        adapter.setDatas(stringList);
    }


    public void refresh(List<PersonInfoEntity> stringList) {
        binding.emptyView.setVisibility(View.GONE);
        binding.rlvList.setVisibility(View.VISIBLE);
        binding.rlvList.setLoadingMoreEnabled(false);
        adapter.setDatas(stringList);
        binding.rlvList.refreshComplete();
        if (adapter.getItemCount() >= stringList.size()) {
            binding.rlvList.setLoadingMoreEnabled(false);
        }
    }

    public void deleteOk(int position) {
//        stringList.remove(position);
//        adapter.removeItem(position);
        viewModel.refresh(map);
    }

    public void loadMore(List<PersonInfoEntity> stringList) {
        adapter.addDatas(stringList);
        binding.rlvList.loadMoreComplete();
        if (adapter.getItemCount() >= stringList.size()) {
            binding.rlvList.setLoadingMoreEnabled(true);
        }
    }

    public void empty() {
        binding.rlvList.refreshComplete();
        binding.emptyView.setVisibility(View.VISIBLE);
    }

    private void showDialog(String userid, int position) {
        View view = View.inflate(getActivity(), R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(getActivity(), 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失
        //roundCornerDialog.setOnKeyListener(keylistener);//设置点击返回键Dialog不消失

        TextView tv_message = view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = view.findViewById(R.id.tv_logout_confirm);
        TextView tv_logout_cancel = view.findViewById(R.id.tv_logout_cancel);
        tv_title.setText("解除关联");
        tv_title.setVisibility(View.VISIBLE);
        tv_message.setText("解除关联后，企业将赋予员工的权限回收，确认解除嘛？");
        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //请求接口
                deletePerson.put("userId", userid);
//                String token = SharedPreferenceUtil.getInstance().getSaveStringData(SharedPreferenceUtil.LOGIN_TOKEN, "");
                viewModel.personDelete(deletePerson, position);
                roundCornerDialog.dismiss();
            }
        });
        //取消
        tv_logout_cancel.setTextColor(getResources().getColor(R.color.color_333333));
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });
    }
}
